package com.billjc.hrm.action;

import static com.opensymphony.xwork2.Action.SUCCESS;

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.billjc.hrm.base.IbatisSessionFactory;
import com.billjc.hrm.model.Employee;
import com.billjc.hrm.model.ParamArray;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("json-default")
@Results( { @Result(name = SUCCESS, location = "/employee-list.jsp") })
public class EmployeeAction extends ActionSupport {
	private static final long serialVersionUID = 1162851697233311327L;
	private Employee employee = null;
	private Integer id = null;
	private String elementid = null;
	private String elementvalue = null;
	private ParamArray param = new ParamArray();
	private SqlSessionFactory sessionfactory = IbatisSessionFactory
			.getSqlSessionFactory();

	public String list() {
		return SUCCESS;
	}

	public String input() throws Exception {
		Map<String, Object> map = ActionContext.getContext().getParameters();
		SqlSession session = sessionfactory.openSession();
//		职员id
		param.setId(id);
//		字段名
		param.setParamName(elementid);
//		对应字段名的值
		param.setParamValue(elementvalue);
		session.update("employee.employeeSingleInfo", param);
		session.commit();
		session.close();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		PrintWriter out;
		out = response.getWriter();
		if ("" == elementvalue) {
//			如果不用空格，则页面值为"click to edit"
			out.print(" ");
		} else {
//			将页面传过来的值回传
			out.print(elementvalue);
		}
		return null;
		// return INPUT;
	}

	public String execute() {
		return list();
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public String getElementid() {
		return elementid;
	}

	public void setElementid(String elementid) {
		this.elementid = elementid;
	}

	public String getElementvalue() {
		return elementvalue;
	}

	public void setElementvalue(String elementvalue) {
		this.elementvalue = elementvalue;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

}
